﻿using DevelopAssistant.Service;
using ICSharpCode.WinFormsUI.Docking;
using NORM.DataBase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.Core.DBMS
{
    public partial class TableObjectsDocument : DockContent
    {
        string tableName;
        DataBaseServer dataBaseServer;


        public TableObjectsDocument()
        {
            InitializeComponent();
            InitializeControls();
        }

        public TableObjectsDocument(string name, DataBaseServer server)
            : this()
        {

            this.tableName = name;
            this.dataBaseServer = server;

            if (AppSettings.WindowTheme != null)
            {
                Type type = AppSettings.WindowTheme.GetType();
                if (type == typeof(ICSharpCode.WinFormsUI.Theme.ThemeVS2012))
                {
                    tabControl1.Radius = 1;
                    tabControl1.ItemSize = new Size(80, 28);
                }
                else if (type == typeof(ICSharpCode.WinFormsUI.Theme.ThemeMac))
                {
                    tabControl1.Radius = 10;
                    tabControl1.ItemSize = new Size(90, 24);
                }
                else if (type == typeof(ICSharpCode.WinFormsUI.Theme.ThemeShadow))
                {
                    tabControl1.Radius = 8;
                    tabControl1.ItemSize = new System.Drawing.Size(84, 26);
                }
            }
        }

        private void InitializeControls()
        {
            tabControl1.Radius = 1;
            tabControl1.ShowClose = false;
            tabControl1.TabCaptionLm = 4;
            tabControl1.ItemSize = new System.Drawing.Size(84, 26);
            tabControl1.BorderColor = SystemColors.ControlDark;
            //tabControl1.ArrowColor = Color.White;
            tabControl1.BaseColor = SystemColors.Control;
            tabControl1.BackColor = SystemColors.Control;
            tabControl1.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
        }

        private void TableObjectsDocument_Load(object sender, EventArgs e)
        {
            System.Threading.Tasks.Task.Run(() => {

                using (var db = Utility.GetAdohelper(this.dataBaseServer))
                {
                    CountDownLatch cdl = new CountDownLatch(3);

                    ThreadPool.QueueUserWorkItem(new WaitCallback((object stateInfo) =>
                    {
                        GetTableIndexes(this.dataGridView1, db);
                        //任务处理完上报状态 完成
                        CountDownLatch counterDownLatch = (CountDownLatch)stateInfo;
                        counterDownLatch.CountDown();

                    }), cdl);

                    ThreadPool.QueueUserWorkItem(new WaitCallback((object stateInfo) =>
                    {
                        GetTableTrigger(this.dataGridView2, db);
                        //任务处理完上报状态 完成
                        CountDownLatch counterDownLatch = (CountDownLatch)stateInfo;
                        counterDownLatch.CountDown();

                    }), cdl);

                    ThreadPool.QueueUserWorkItem(new WaitCallback((object stateInfo) =>
                    {
                        GetTableConstraint(this.dataGridView3, db);
                        //任务处理完上报状态 完成
                        CountDownLatch counterDownLatch = (CountDownLatch)stateInfo;
                        counterDownLatch.CountDown();

                    }), cdl);

                    cdl.Await();

                }
            
            });
        }

        private void GetTableIndexes(DataGridView datagridview, DataBase db)
        {
            using (DataTable dt = db.GetTableIndexes(this.tableName))
            {
                this.Invoke(new MethodInvoker(delegate()
                {
                    datagridview.DataSource = dt;
                }));              
            }
            
        }

        private void GetTableTrigger(DataGridView datagridview, DataBase db)
        {
            using (DataTable dt = db.GetTableTrigger(this.tableName))
            {
                this.Invoke(new MethodInvoker(delegate()
                {
                    datagridview.DataSource = dt;
                }));
            }            
        }

        private void GetTableConstraint(DataGridView datagridview, DataBase db)
        {
            using (DataTable dt = db.GetTableConstraint(this.tableName))
            {
                this.Invoke(new MethodInvoker(delegate() { datagridview.DataSource = dt; }));
            }           
        }

    }
}
